﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Threading;

namespace ProcessingLite
{
    public class DefaultProjectExecutionService : IProjectExecutionService
    {
        public DefaultProjectExecutionService()
        {
        }

        public void ExecuteProject(IProcessingLiteDrawingProject project, IProcessingLiteDrawingService drawer)
        {
            ParameterizedThreadStart startMethod = new ParameterizedThreadStart(this.ExecuteProject);
            Thread executionThread = new Thread(startMethod);
            executionThread.SetApartmentState(ApartmentState.STA);
            executionThread.Start(new ProjectStartupContext(drawer, project));
            drawer.Window.ShowDialog();
            project.DrawStuff(drawer);
        }

        private void ExecuteProject(object startupContext)
        {
            ProjectStartupContext context = (ProjectStartupContext)startupContext;
            context.Project.DrawStuff(context.Drawer);
        }


        private class ProjectStartupContext
        {
            public ProjectStartupContext(IProcessingLiteDrawingService drawer, IProcessingLiteDrawingProject project)
            {
                Drawer = drawer;
                Project = project;
            }

            public IProcessingLiteDrawingService Drawer 
            { 
                get; 
                private set; 
            }
            public IProcessingLiteDrawingProject Project 
            { 
                get; 
                private set; 
            }
        }
    }
}
